<template>
  <div v-if="!item.children[0].meta || !item.children[0].meta.webHidden " class="bg-transparent">
    <!--
      hasOneShowingChild(item.children,item)
      return {Boolean}:
        item 路由对象中, children 数组（子路由）中  " 是否只有 1个 要展示的路由 或 没有要展示的路由 "，

        条件成立 返回 true  路由对象          赋值给    this.onlyOneChild

        否侧     返回 false 将 item(路由对象) 赋值给    this.onlyOneChild,
                 并给 this.onlyOneChild 加上 属性 noShowingChildren: true,
                 表示 item 路由对象中 没有子路由要展示

        (!onlyOneChild.children || onlyOneChild.noShowingChildren)
        !onlyOneChild.children：          判断  onlyOneChild  路由对象 没有 子集路由              （隐含前提条件是，onlyOneChild 不是 item 路由对象本身）
        onlyOneChild.noShowingChildren：  判断  onlyOneChild  路由对象 noShowingChildren 为 true  （隐含前提条件是，onlyOneChild 是 item 路由对象本身）
    -->
    <template
      v-if="
        !item.alwaysShow &&
        hasOneShowingChild(item.children, item) &&
        (!onlyOneChild.children || onlyOneChild.noShowingChildren)
      "
    >
      <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
        <el-menu-item :index="resolvePath(onlyOneChild.path)">
          <!-- <item
            :icon="onlyOneChild.meta.icon || (item.meta && item.meta.icon)"
            :title="onlyOneChild.meta.title"
          /> -->
          {{ onlyOneChild.meta.title }}
        </el-menu-item>
      </app-link>
    </template>

    <el-submenu
      v-else
      ref="subMenu"
      :index="resolvePath(item.path)"
      popper-append-to-body
      class="bg-whtie hover:bg-white"
    >
      <!-- <item
        :icon="item.meta.icon || (item.meta && item.meta.icon)"
        :title="item.meta.title"
      /> -->
      <span slot="title"> {{item.meta.title}} </span>
      <!-- {{ item.meta.title}} -->
      <navbar-item
        v-for="(child,index) in item.children"
        :key="index"
        :is-nest="true"
        :item="child"
        :base-path="resolvePath(child.path)"
        class="nest-menu"
      />
    </el-submenu>
  </div>
</template>

<script>
import path from "path";
import { isExternal } from "@/utils/validate";
import Item from "./Item";
import AppLink from "./Link";
import FixiOSBug from "./FixiOSBug";

export default {
  name: "NavbarItem",
  components: { Item, AppLink },
  mixins: [FixiOSBug],
  props: {
    // route object
    item: {
      type: Object,
      required: true,
    },
    isNest: {
      type: Boolean,
      default: false,
    },
    basePath: {
      type: String,
      default: "",
    },
  },
  data() {
    // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
    // TODO: refactor with render function
    this.onlyOneChild = null;
    return {};
  },
  methods: {
    hasOneShowingChild(children = [], parent) {
      const showingChildren = children.filter((item) => {
        if (item.meta && item.meta.webHidden) {
          return false;
        } else {
          // Temp set(will be used if only has one showing child)
          this.onlyOneChild = item;
          return true;
        }
      });

      // When there is only one child router, the child router is displayed by default
      if (showingChildren.length === 1) {
        return true;
      }

      // Show parent if there are no child router to display
      if (showingChildren.length === 0) {
        this.onlyOneChild = { ...parent, path: "", noShowingChildren: true };
        return true;
      }

      return false;
    },
    resolvePath(routePath) {
      if (isExternal(routePath)) {
        return routePath;
      }
      if (isExternal(this.basePath)) {
        return this.basePath;
      }
      return path.resolve(this.basePath, routePath);
    },
  },
};
</script>
